import { defineConfig } from 'rollup';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import path from 'path';

// 通用配置
const commonConfig = (packagePath) => ({
  plugins: [
    resolve({
      preferBuiltins: true,
      extensions: ['.ts', '.js']
    }),
    commonjs(),
    // 简化TypeScript插件配置
    typescript({
      tsconfig: false, // 不使用项目级tsconfig，使用内联配置
      compilerOptions: {
        module: 'ESNext',
        target: 'ESNext',
        sourceMap: true,
        declaration: false // 类型定义将通过build.js中的单独步骤生成
      },
      include: ['*.ts+(|x)', '**/*.ts+(|x)'],
      exclude: ['node_modules', 'dist', '**/*.test.ts', '**/*.spec.ts']
    }),
    terser()
  ],
  external: [] // 外部依赖，避免打包进bundle
});

// 组件包配置
export const componentsConfig = defineConfig({
  ...commonConfig('./packages/components'),
  input: './packages/components/src/index.ts',
  output: [
    {
      file: './packages/components/dist/index.js',
      format: 'cjs',
      sourcemap: true
    },
    {
      file: './packages/components/dist/index.esm.js',
      format: 'esm',
      sourcemap: true
    }
  ]
});

// 工具包配置
export const utilsConfig = defineConfig({
  ...commonConfig('./packages/utils'),
  input: './packages/utils/src/index.ts',
  output: [
    {
      file: './packages/utils/dist/index.js',
      format: 'cjs',
      sourcemap: true
    },
    {
      file: './packages/utils/dist/index.esm.js',
      format: 'esm',
      sourcemap: true
    }
  ]
});

// 导出配置数组
export default [componentsConfig, utilsConfig];